<?php
/**
 * Slipstream :: Highly adaptive dependency injection framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Slipstream\Infrastructure\Context\Relevance;

use Slipstream\Infrastructure\Context\Type\ResolverServiceInterface;
use Slipstream\Configuration\ContextInterface;

/**
 * Analyzer that determines the context's weight by comparing how the target
 * context's type inherits from the context's type
 *
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
class TypeInheritanceAnalyzer implements AnalyzerInterface
{
	const WEIGHT_EQUAL_TYPE = 900;
	const WEIGHT_PARENT = 700;
	const WEIGHT_PARENT_DISTANCE_PENALTY = 10;
	const WEIGHT_INTERFACE = 500;
	const WEIGHT_INTERFACE_DISTANCE_PENALTY = 10;
	const WEIGHT_NO_INHERITANCE = 0;
	
	/**
	 * @var ResolverServiceInterface
	 */
	protected $typeResolver;
	
	/**
	 * Class constructor
	 *
	 * @param ResolverServiceInterface $typeResolver
	 * @return void
	 */
	public function __construct(ResolverServiceInterface $typeResolver)
	{
		$this->typeResolver = $typeResolver;
	}
	
	/**
	 * Returns the relevance weight of a given context in terms of the target
	 * context
	 *
	 * @param ContextInterface $context
	 * @param ContextInterface $targetContext
	 * @return int|false Returns false if relevance can't be determined
	 */
	public function getWeight(ContextInterface $context, ContextInterface $targetContext)
	{
		$resolvedType = $this->typeResolver->resolveType($context);
		$resolvedTargetType = $this->typeResolver->resolveType($targetContext);
		
		// Could not resolve type for either the context or target context
		if(!$resolvedType || !$resolvedTargetType) {
			return false;
		}
		
		// Context's type and target context's type are the same
		if(strcasecmp($resolvedType, $resolvedTargetType) === 0) {
			return self::WEIGHT_EQUAL_TYPE;
		}
		
		// Context type is a parent type of the target context's type
		$parentTypes = class_parents($resolvedTargetType);
		if(is_array($parentTypes)) {
			$parentTypes = array_values($parentTypes);
			foreach($parentTypes as $distance => $parentType) {
				if(strcasecmp($resolvedType, $parentType) === 0) {
					return (self::WEIGHT_PARENT - (self::WEIGHT_PARENT_DISTANCE_PENALTY * $distance));
				}
			}
		}
		
		// Context type is an interface type that is implemented by the target context's type
		$interfaceTypes = class_implements($resolvedTargetType);
		if(is_array($interfaceTypes)) {
			$interfaceTypes = array_values($interfaceTypes);
			foreach($interfaceTypes as $distance => $interfaceType) {
				if(strcasecmp($resolvedType, $interfaceType) === 0) {
					return (self::WEIGHT_INTERFACE - (self::WEIGHT_INTERFACE_DISTANCE_PENALTY * $distance));
				}
			}
		}
		
		// No inheritance - context has no relevance to target context
		return self::WEIGHT_NO_INHERITANCE;
	}
}